/**
 * 限制僅限數字輸入
 * 使用方法：<input type="text" ... onkeypress="setNumConstraint(event, this)">
 * @param	obj 使用物件
 * @Return	字元
 */
function setNumConstraint(e, obj){
	var iKeyCode = window.event?e.keyCode:e.which;
	var isIE = false;
	var isFF = false;
	
	/*
	 * ie9: parseInt(navigator.appVersion)=5
	 * ie6: parseInt(navigator.appVersion)=4
	 * ie9的event跟firefox的物件為同一物件型態，故parseInt(navigator.appVersion)限定ie8以下
	 */
//	if ((navigator.userAgent.indexOf("MSIE")>0) && (parseInt(navigator.appVersion) >= 4)){
	if ((navigator.userAgent.indexOf("MSIE")>0) && (parseInt(navigator.appVersion) == 4)){
		isIE = true;
	}
	
	if (navigator.userAgent.indexOf("Firefox")>0){
		isFF = true;
	}
	//if(!(((iKeyCode >= 48) && (iKeyCode <= 57)) || (iKeyCode == 13) || (iKeyCode == 46) || (iKeyCode == 45) || (iKeyCode == 37) || (iKeyCode == 39) || (iKeyCode == 8) || (iKeyCode == 47))){
	
	if(iKeyCode == 45){
		if(obj.value.indexOf("-")>=0){
			if (isIE){
				e.returnValue = false;
			}else{
				e.preventDefault();   //重要 firefox是用prevenDefault()来取消事件的
			}
		}else{
			if (isIE){
				e.returnValue = false;
			}else{
				e.preventDefault();   //重要 firefox是用prevenDefault()来取消事件的
			}
			obj.value = "-" + obj.value;
		}
		return false;
	}
	
	if(iKeyCode == 46 && obj.value.indexOf(".")>=0){
		if (isIE){
			e.returnValue = false;
		}else{
			e.preventDefault();   //重要 firefox是用prevenDefault()来取消事件的
		}
		return false;
	}
	if(!(((iKeyCode >= 48) && (iKeyCode <= 57)) || (iKeyCode == 45 || iKeyCode == 46))){
		if (isIE){
			e.returnValue = false;
		}else{
			e.preventDefault();   //重要 firefox是用prevenDefault()来取消事件的
		}
	}
}

function initNumber(event, obj){
	//游標位置會跑, 所以暫時先不用
	//obj.value = obj.value.replace(/\,/g, "");
}

function formatNumber(event, obj){
	var value = obj.value;
	value = value.replace(/[^0-9.-]/g,'');
	value = value.replace(/\,/g, "");
	
	var temp = "";
	var tempPoint = "";
	value = String(value);
	if(value.indexOf("-")>0){
		temp = "-";
		value = value.substring(1);
	}

	if(value.indexOf(".")>0){
		tempPoint = value.substring(value.indexOf("."));
		value = value.substring(0, value.indexOf("."));
	}
	var re=/(-?\d+)(\d{3})/;
	if(value != ""){
		value = String(Number(value));
		while(re.test(value)){ 
			value = value.replace(re, "$1,$2");
		}
		obj.value =  temp + value + tempPoint;
	}
}

function checkConstraint(e, obj){
	if((obj.value.length == 1) && (obj.value.substring(0,1) == "-")){
		alert("請輸入正確數值!");
		obj.value = "";
		return;
	}
}
